package com.yangsen.index;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.upload.UploadFile;
import com.yangsen.common.model.User;
import com.yangsen.login.LoginService;
import com.yangsen.oss.OSSService;
import com.yangsen.oss.filePath;

import java.io.File;
import java.util.List;

public class IndexController extends Controller {

    private OSSService ossService = new OSSService();

    public void index() {
        render("index.html");
    }

    public void upload(String path) {
        String token = this.getCookie(LoginService.USER);
        User user = token == null ? null : Redis.use().get(token);
        if (null != user) {
            System.out.println(path);
            UploadFile uploadFile = getFile();
            File file = uploadFile.getFile();
            System.out.println(path);
            path = getPara("test");
            System.out.println(path);
            if (path == null) {
                path = user.getUsername() + "/";
            }
            if (ossService.upload(file.getName(), file, path)) {
                file.delete();
                renderJson(Ret.by("status", true).set("msg", "成功"));
            } else {
                file.delete();
                renderJson(Ret.by("status", false).set("msg", "未知错误"));
            }
        } else {
            renderJson(Ret.by("status", false).set("msg", "未知用户"));
        }
    }

    public void listObject(String folderName) {
        String token = this.getCookie(LoginService.USER);
        User user = token == null ? null : Redis.use().get(token);
        if (null != user) {
//            try {
//                getResponse().getWriter().print(ossService.listObject(user.getUsername()));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            if (folderName == null) {
                renderJson(ossService.listObject(user.getUsername() + "/"));
            } else {
//                System.out.println("1");
//                System.out.println(folderName);
                renderJson(ossService.listObject(folderName));
            }

        } else {
            renderNull();
        }
    }

    public void breadcrumb(String path) {
        String token = this.getCookie(LoginService.USER);
        User user = token == null ? null : Redis.use().get(token);
        if (null != user) {
//            try {
//                getResponse().getWriter().print(ossService.listObject(user.getUsername()));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            if (path == null) {
                renderJson(filePath.filePathForBread(user.getUsername() + "/"));
            } else {
//                System.out.println("1");
//                System.out.println(folderName);
                renderJson(filePath.filePathForBread(path));
            }

        } else {
            renderNull();
        }
    }

    public void newFolder(String folderName) {
//        System.out.println(folderName);

        String token = this.getCookie(LoginService.USER);
        User user = token == null ? null : Redis.use().get(token);
        if (null != user) {
            if (ossService.createFolder(user.getUsername(), folderName)) {
                renderJson(Ret.by("status", true).set("msg", "成功"));
            } else {
                renderJson(Ret.by("status", false).set("msg", "未知错误"));
            }
        } else {
            renderNull();
        }

    }

    public void deleteOnly(String key) {
        System.out.println(key);
        if (ossService.deleteOnly(key)) {
            renderJson(Ret.by("status", true).set("msg", "成功"));
        } else {
            renderJson(Ret.by("status", false).set("msg", "未知错误"));
        }
    }

    public void deleteTest(String data) {


        List<String> deleteKeys = JSONArray.parseArray(data, String.class);
        if (ossService.deleteMany(deleteKeys)) {
            renderJson(Ret.by("status", true).set("msg", "成功"));
        } else {
            renderJson(Ret.by("status", false).set("msg", "未知错误"));
        }
    }

    public void download(String key) {
        if (ossService.download(key)) {
            String temp[] = key.split("/");
            File file = new File("D:/Java Projects/senyun/" + temp[temp.length - 1]);
            if (file.exists()) {
//                renderFile(file);
//                file.delete();
                render(new TempFileRender(file));
            } else {
                renderJson(Ret.by("status", false).set("msg", "in"));
            }

        } else {
            renderJson(Ret.by("status", false));
        }
    }
}
